package com.cv4j.app.slice;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.ability.pixels.PixelOperatorAbility;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utils.ClickUtil;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

/**
 * ArithmeticAndLogicOperationSlice
 *
 * @since 2021-05-14
 */
public class ArithmeticAndLogicOperationSlice extends BaseAbilitySlice implements Component.ClickedListener {
    private Text text1;
    private Text text2;
    private Text text3;
    private Text text4;
    private Text text5;
    private Text text6;
    private Text text7;
    private Text text8;
    private Text text9;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_arithmetic_and_logic_operation;
    }

    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    private void initView() {
        text1 = (Text) findComponentById(ResourceTable.Id_operation_text1);
        text1.setClickedListener(this);
        text2 = (Text) findComponentById(ResourceTable.Id_operation_text2);
        text2.setClickedListener(this);
        text3 = (Text) findComponentById(ResourceTable.Id_operation_text3);
        text3.setClickedListener(this);
        text4 = (Text) findComponentById(ResourceTable.Id_operation_text4);
        text4.setClickedListener(this);
        text5 = (Text) findComponentById(ResourceTable.Id_operation_text5);
        text5.setClickedListener(this);
        text6 = (Text) findComponentById(ResourceTable.Id_operation_text6);
        text6.setClickedListener(this);
        text7 = (Text) findComponentById(ResourceTable.Id_operation_text7);
        text7.setClickedListener(this);
        text8 = (Text) findComponentById(ResourceTable.Id_operation_text8);
        text8.setClickedListener(this);
        text9 = (Text) findComponentById(ResourceTable.Id_operation_text9);
        text9.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (ClickUtil.isFastClick()){
            return;
        }
        switch (component.getId()) {
            case ResourceTable.Id_operation_text1: // 加操作
                jumpToAbility(PixelOperatorAbilitySlice.ADD, (Text) component);
                break;
            case ResourceTable.Id_operation_text2: // 减操作
                jumpToAbility(PixelOperatorAbilitySlice.SUBSTRACT, (Text) component);
                break;
            case ResourceTable.Id_operation_text3: // 乘操作
                jumpToAbility(PixelOperatorAbilitySlice.MULTIPLE, (Text) component);
                break;
            case ResourceTable.Id_operation_text4: // 除操作
                jumpToAbility(PixelOperatorAbilitySlice.DIVISION, (Text) component);
                break;
            case ResourceTable.Id_operation_text5: // 和操作
                jumpToAbility(PixelOperatorAbilitySlice.BITWISE_AND, (Text) component);
                break;
            case ResourceTable.Id_operation_text6: // 或操作
                jumpToAbility(PixelOperatorAbilitySlice.BITWISE_OR, (Text) component);
                break;
            case ResourceTable.Id_operation_text7: // 非操作
                jumpToAbility(PixelOperatorAbilitySlice.BITWISE_NOT, (Text) component);
                break;
            case ResourceTable.Id_operation_text8: // 异或操作
                jumpToAbility(PixelOperatorAbilitySlice.BITWISE_XOR, (Text) component);
                break;
            case ResourceTable.Id_operation_text9: // 像素混合
                jumpToAbility(PixelOperatorAbilitySlice.ADD_WEIGHT, (Text) component);
                break;
            default:
                break;
        }
    }

    /**
     * jumpToAbility
     *
     * @param add add
     * @param text text
     */
    public void jumpToAbility(int add, Text text) {
        Intent intent = new Intent();
        intent.setParam("Title", text.getText());
        intent.setParam("Type", add);
        intent.setOperation(new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(PixelOperatorAbility.class.getName())
            .build());
        startAbility(intent);
    }
}
